﻿using System;

namespace ImTryin.StateMachineLanguage
{
    public partial class TransitionConnectorBase
    {
        private string GetTextValue()
        {
            Transition transition = (Transition) ModelElement;

            if ( transition == null )
                return string.Empty;

            return string.Format( "{0}{3}[ {1} ]{3}/ {2}", transition.Trigger, transition.Condition, transition.Action,
                                  /*Environment.NewLine*/ " " );
        }

        private void SetTextValue( string newValue )
        {
            Transition transition = (Transition) ModelElement;

            if ( transition == null )
                return;

            if ( ( newValue == null ) || ( newValue.Trim() == string.Empty ) )
            {
                transition.Trigger = string.Empty;
                transition.Condition = string.Empty;
                transition.Action = string.Empty;
                return;
            }

            string action = string.Empty;

            int actionIndex = newValue.LastIndexOf( "/" );
            if ( actionIndex >= 0 )
            {
                action = newValue.Substring( actionIndex + 1 ).Trim();

                newValue = newValue.Substring( 0, actionIndex );
            }

            string condition = string.Empty;

            int conditionBegin = newValue.IndexOf( "[" );
            int conditionEnd = newValue.LastIndexOf( "]" );

            if ( ( conditionBegin >= 0 ) && ( conditionBegin < conditionEnd ) )
            {
                condition = newValue.Substring( conditionBegin + 1, conditionEnd - conditionBegin - 1 ).Trim();

                newValue = newValue.Substring( 0, conditionBegin );
            }

            transition.Trigger = newValue.Trim();
            transition.Condition = condition;
            transition.Action = action;
        }
    }
}